package com.returnsoft.softphone.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.softphone.dto.PeerDto;
import com.returnsoft.softphone.eao.PeerEao;
import com.returnsoft.softphone.entity.Peer;
import com.returnsoft.softphone.exception.ConversionExcepcion;
import com.returnsoft.softphone.exception.CCenterExcepcion;
import com.returnsoft.softphone.exception.EaoExcepcion;
import com.returnsoft.softphone.util.Conversion;


/**
 * Session Bean implementation class PeerBean
 */
@Stateless
@LocalBean
// @WebService
public class PeerBean implements PeerInterface , Serializable{
	private static final long serialVersionUID = 1L;
	@EJB
	private PeerEao peerEao;
	@EJB
	private Conversion conv;

	@Override
	public List<PeerDto> findAvailables(Integer serverId)
			throws CCenterExcepcion {
		try {
			List<Peer> peers = peerEao.findAvailables(serverId);
			List<PeerDto> peersDto = new ArrayList<PeerDto>();
			for (Peer peer : peers) {
				PeerDto pd = conv.fromPeer(peer);
				peersDto.add(pd);
			}
			return peersDto;

		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}
	}
	
	
	

	// //////////////////////////////////////////////////////////////////////////////

	/*public Integer updatePeer(PeerDto peerDto) {

		try {
			System.out.println("inicia updatePeer en el ejb");
			System.out.println(peerDto.getAgent().getId());
			Peer peer = conv.toPeer(peerDto);
			System.out.println(peer.getAgent().getId());
			Integer result = peerEao.updatePeer(peer);
			System.out.println("finaliza updatePeer en el ejb");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}*/
	
	


	
}
